const fs = require("fs");
const fp = require("lodash/fp");

class IO {
  static of(value) {
    return new IO(function () {
      return value;
    });
  }

  constructor(fn) {
    this._value = fn;
  }

  map(fn) {
    return new IO(fp.flowRight(fn, this._value));
  }
}

let readFile = (filename) => {
  return new IO(() => {
    return fs.readFileSync(filename, "utf-8");
  });
};

let print = (str) => {
  return new IO(() => {
    console.log(str);
    return str;
  });
};

let cat = fp.flowRight(print, readFile);
console.log(cat("package.json")._value()._value());
